#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class AccidentRcdProvider : CY.GFive.Core.Providers.DALProvider.IAccidentRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectAccidentRcd = "SELECT [Id], [DevCode],[DevUnitCode],[ClsRoomCode],[AccidentDate],[ReportDate],[Reason],[AccinenterName],[ReporterName],[LeaderName] FROM [AccidentRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertAccidentRcd = "INSERT INTO [AccidentRcd]([DevCode],[DevUnitCode],[ClsRoomCode],[AccidentDate],[ReportDate],[Reason],[AccinenterName],[ReporterName],[LeaderName]) values(@DevCode,@DevUnitCode,@ClsRoomCode,@AccidentDate,@ReportDate,@Reason,@AccinenterName,@ReporterName,@LeaderName);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateAccidentRcd = "UPDATE [AccidentRcd] SET [DevCode] = @DevCode,[DevUnitCode] = @DevUnitCode,[ClsRoomCode] = @ClsRoomCode,[AccidentDate] = @AccidentDate,[ReportDate] = @ReportDate,[Reason] = @Reason,[AccinenterName] = @AccinenterName,[ReporterName] = @ReporterName,[LeaderName] = @LeaderName WHERE  [Id]=@Id";

		private static readonly string SqlDeleteAccidentRcd = "DELETE [AccidentRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllAccidentRcd = "SELECT [Id], [DevCode],[DevUnitCode],[ClsRoomCode],[AccidentDate],[ReportDate],[Reason],[AccinenterName],[ReporterName],[LeaderName] FROM [AccidentRcd]";
		#endregion

		#region IAccidentRcdProvider Members
		public Core.Business.AccidentRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectAccidentRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.AccidentRcd accidentRcd = new Core.Business.AccidentRcd();

				if (!reader.IsDBNull(0)) accidentRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) accidentRcd.DevCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) accidentRcd.DevUnitCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) accidentRcd.ClsRoomCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) accidentRcd.AccidentDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) accidentRcd.ReportDate = reader.GetDateTime(5);
				if (!reader.IsDBNull(6)) accidentRcd.Reason = reader.GetString(6);
				if (!reader.IsDBNull(7)) accidentRcd.AccinenterName = reader.GetString(7);
				if (!reader.IsDBNull(8)) accidentRcd.ReporterName = reader.GetString(8);
				if (!reader.IsDBNull(9)) accidentRcd.LeaderName = reader.GetString(9);

				return accidentRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.AccidentRcd accidentRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DevCode", SqlDbType.NVarChar, accidentRcd.DevCode);
			sql.AddParameter("@DevUnitCode", SqlDbType.NVarChar, accidentRcd.DevUnitCode);
			sql.AddParameter("@ClsRoomCode", SqlDbType.NVarChar, accidentRcd.ClsRoomCode);
			sql.AddParameter("@AccidentDate", SqlDbType.DateTime, accidentRcd.AccidentDate);
			sql.AddParameter("@ReportDate", SqlDbType.DateTime, accidentRcd.ReportDate);
			sql.AddParameter("@Reason", SqlDbType.NVarChar, accidentRcd.Reason);
			sql.AddParameter("@AccinenterName", SqlDbType.NVarChar, accidentRcd.AccinenterName);
			sql.AddParameter("@ReporterName", SqlDbType.NVarChar, accidentRcd.ReporterName);
			sql.AddParameter("@LeaderName", SqlDbType.NVarChar, accidentRcd.LeaderName);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertAccidentRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) accidentRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.AccidentRcd accidentRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DevCode", SqlDbType.NVarChar, accidentRcd.DevCode);
			sql.AddParameter("@DevUnitCode", SqlDbType.NVarChar, accidentRcd.DevUnitCode);
			sql.AddParameter("@ClsRoomCode", SqlDbType.NVarChar, accidentRcd.ClsRoomCode);
			sql.AddParameter("@AccidentDate", SqlDbType.DateTime, accidentRcd.AccidentDate);
			sql.AddParameter("@ReportDate", SqlDbType.DateTime, accidentRcd.ReportDate);
			sql.AddParameter("@Reason", SqlDbType.NVarChar, accidentRcd.Reason);
			sql.AddParameter("@AccinenterName", SqlDbType.NVarChar, accidentRcd.AccinenterName);
			sql.AddParameter("@ReporterName", SqlDbType.NVarChar, accidentRcd.ReporterName);
			sql.AddParameter("@LeaderName", SqlDbType.NVarChar, accidentRcd.LeaderName);
			sql.AddParameter("@Id", SqlDbType.Int, accidentRcd.Id);
			sql.ExecuteSql(SqlUpdateAccidentRcd);
		}

		public void Delete(Core.Business.AccidentRcd accidentRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, accidentRcd.Id);
			sql.ExecuteSql(SqlDeleteAccidentRcd);
		}

		public IList<Core.Business.AccidentRcd> GetAllAccidentRcd()
		{
			IList<Core.Business.AccidentRcd> accidentRcdlist = new List<Core.Business.AccidentRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllAccidentRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.AccidentRcd accidentRcd = new Core.Business.AccidentRcd();

					if (!reader.IsDBNull(0)) accidentRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) accidentRcd.DevCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) accidentRcd.DevUnitCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) accidentRcd.ClsRoomCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) accidentRcd.AccidentDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) accidentRcd.ReportDate = reader.GetDateTime(5);
					if (!reader.IsDBNull(6)) accidentRcd.Reason = reader.GetString(6);
					if (!reader.IsDBNull(7)) accidentRcd.AccinenterName = reader.GetString(7);
					if (!reader.IsDBNull(8)) accidentRcd.ReporterName = reader.GetString(8);
					if (!reader.IsDBNull(9)) accidentRcd.LeaderName = reader.GetString(9);

					accidentRcd.MarkOld();
					accidentRcdlist.Add(accidentRcd);
				}
				reader.Close();
			}
			return accidentRcdlist;
		}
		#endregion
	}
}
